blog

Home / DeveloperSection / Blogs / Threading in c-sharp.

Threading in c-sharp.

Anonymous User7029 25-May-2011

A thread is an independent execution path, able to run simultaneously with other threads. Threading enables your C# program to perform concurrent processing so you can do more than one operation at a time. For example, you can use threading to monitor input from the user, perform background tasks, and handle simultaneous streams of input. The System.Threading namespace provides classes and interfaces that support multithreaded programming and enable you to easily perform tasks such as creating and starting new threads, synchronizing multiple threads, suspending threads, and aborting threads.

By default every application in c# is single threaded, as well as you add new thread then that application is automatically converted in multithread. When you execute your program then first thread is main thread which is created by operating system and clr and that is known as main thread. In c# there is two types of thread

1.       Process Thread: Which have a user interface and which manage interface thread that manages interaction with user.

2.       Worker Thread: Which perform functionality or other tasks.

Following steps needs to be performed while creating thread in c#

1)      We will import System.Threading namespace which provides all necessary classes and interfaces which is important to work with threading.

2)      Create a method which will be treated as a thread and which is passed to the object of ThreadStart delegate.

3)      Create an object of ThreadStart delegate and pass method to the constructor of ThreadStart class.

4)      Create an object of Thread class and pass object of ThreadStart delegate as parameter.

5)      Call the start() method of thread class which start newly created thread.

Example which represents a simple thread


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadStart ths = new ThreadStart(newThread);    //Creating an object of ThreadStart delegate.
            Thread th = new Thread(ths);            //Creating an object of Thread class.
            th.Start();       //Calling start() method of thread class which will start execution of newThread.
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Main Thread");
            }
        }
        /// <summary>
        /// This will work as seperate thread.
        /// </summary>
        public static void newThread()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("New Thread");
            }
        }
    }
}


Output of code snippet

Main Thread

Main Thread

Main Thread

Main Thread

Main Thread

New Thread

New Thread

New Thread

New Thread

New Thread

New Thread

New Thread

New Thread

New Thread

New Thread

Main Thread

Main Thread

Main Thread

Main Thread

Main Thread


Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By